package com.frame.work.lib;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.util.Frame;
import com.frame.work.dto.GatewayTimeTaskReqDTO;
import com.frame.work.dto.GatewayTimeTaskReqPutDTO;
import com.frame.work.entity.TblGatewayTimingScene;
import com.frame.work.mqtt.data.handle.MqttDataHandle;
import com.frame.work.service.TblGatewayTimingSceneService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wjming
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GatewayTimedHandle {
    private final MqttDataHandle mqttDataHandle;
    private final TblGatewayTimingSceneService gatewayTimingSceneService;
    private final RedisTemplate redisTemplate;
    String topic = "zyzn/api/device/gatewayMac/gateway/timedTask";

    public void processFrame(String gatewayMac, String frameString) {
        log.info("[群组配置] -> [processFrame] -> [gatewayMac:{},数据帧:{}]", gatewayMac, frameString);
        String topics = StringUtils.replace(topic, "gatewayMac", gatewayMac);

        String data = Frame.getData(frameString);
        String isTrue = StringUtils.substring(data, data.length() - 2, data.length());
        String random = StringUtils.substring(data, data.length() - 6, data.length() - 2);
        String type = StringUtils.substring(data, data.length() - 8, data.length() - 6);
        String redisKey = gatewayMac + "_" + random;
        Map<String, Object> map = new HashMap<>(2);
        map.put("type", "gateway/timedTask");
        map.put("gatewayMac", gatewayMac);
        map.put("code", isTrue);

        if (!StringUtils.equals("00", isTrue)) {
            mqttDataHandle.basePush(topics, JSONObject.toJSONString(map));
            return;
        }
        Object dto = redisTemplate.opsForValue().get(redisKey);
        if (dto == null) {
            return;
        }
        TblGatewayTimingScene gatewayTimingScene;
        switch (type) {
            case "88":
                GatewayTimeTaskReqDTO gatewayTimeTaskReqDTO = JSON.parseObject(JSON.toJSONString(dto), GatewayTimeTaskReqDTO.class);
                gatewayTimingScene = TblGatewayTimingScene.builder()
                        .gatewayId(gatewayTimeTaskReqDTO.getGatewayId())
                        .taskEnable(gatewayTimeTaskReqDTO.getTaskEnable())
                        .taskIndex(gatewayTimeTaskReqDTO.getTaskIndex())
                        .startTime(gatewayTimeTaskReqDTO.getStartTime())
                        .taskInterval(gatewayTimeTaskReqDTO.getTaskInterval())
                        .repeatTime(gatewayTimeTaskReqDTO.getRepeatTime())
                        .sceneId(gatewayTimeTaskReqDTO.getDealId())
                        .build();
                gatewayTimingSceneService.save(gatewayTimingScene);
                map.put("timedTask", gatewayTimeTaskReqDTO.getTaskIndex());
                break;
            case "89":
                String id = dto.toString();
                TblGatewayTimingScene byId = gatewayTimingSceneService.getById(id);
                gatewayTimingSceneService.removeById(id);
                map.put("timedTask", byId.getTaskIndex());
                break;
            case "8B":
                GatewayTimeTaskReqPutDTO gatewayTimeTaskReqPutDTO = JSON.parseObject(JSON.toJSONString(dto), GatewayTimeTaskReqPutDTO.class);
                gatewayTimingScene = TblGatewayTimingScene.builder().taskEnable(gatewayTimeTaskReqPutDTO.getTaskEnable()).build();
                gatewayTimingScene.setId(gatewayTimeTaskReqPutDTO.getId());
                gatewayTimingSceneService.updateById(gatewayTimingScene);
                map.put("timedTask", gatewayTimeTaskReqPutDTO.getTaskIndex());
                break;
            case "8D":
                gatewayTimeTaskReqPutDTO = JSON.parseObject(JSON.toJSONString(dto), GatewayTimeTaskReqPutDTO.class);
                gatewayTimingScene = TblGatewayTimingScene.builder()
                        .taskEnable(gatewayTimeTaskReqPutDTO.getTaskEnable())
                        .taskEnable(gatewayTimeTaskReqPutDTO.getTaskEnable())
                        .taskIndex(gatewayTimeTaskReqPutDTO.getTaskIndex())
                        .startTime(gatewayTimeTaskReqPutDTO.getStartTime())
                        .taskInterval(gatewayTimeTaskReqPutDTO.getTaskInterval())
                        .repeatTime(gatewayTimeTaskReqPutDTO.getRepeatTime())
                        .sceneId(gatewayTimeTaskReqPutDTO.getDealId())
                        .build();
                gatewayTimingScene.setId(gatewayTimeTaskReqPutDTO.getId());
                gatewayTimingSceneService.updateById(gatewayTimingScene);
                map.put("timedTask", gatewayTimeTaskReqPutDTO.getTaskIndex());
                break;
            default:
                break;
        }
        mqttDataHandle.basePush(topics, JSONObject.toJSONString(map));
    }

}
